package web;

import core.AjaxResult;
import core.BaseHttpServlet;
import dao.UserDao;
import dao.impl.UserDaoImpl;
import domain.AdminUser;
import service.AdminUserService;
import service.impl.AdminUserServiceImpl;
import util.RequestUtils;
import util.ResponseUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 于金鹏
 * @Date 2024/12/22 15:03
 * @Package StudentCheckSystem
 */
@WebServlet("/user/*")
public class UserServlet extends BaseHttpServlet {
    private AdminUserService adminUserService;

    @Override
    public void init() throws ServletException {
        adminUserService = new AdminUserServiceImpl(new UserDaoImpl());
    }

    // 根据id查询
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getParameterMap().containsKey("admin_id")) {
            ResponseUtils.responseJSON(resp, AjaxResult.error(400, "请传入admin_id"));
            return;
        }
        int id = Integer.parseInt(req.getParameter("admin_id"));
        executeBusiness(resp, response -> {
            AdminUser adminUser = adminUserService.getUserById(id);
            ResponseUtils.responseJSON(resp, AjaxResult.success(adminUser));
        });
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        if (pathInfo == null) {
            AdminUser adminUser = RequestUtils.getReqJsonBody(req, AdminUser.class);
            executeBusiness(resp, response -> {
                ResponseUtils.responseJSON(resp, AjaxResult.success(adminUserService.addUser(adminUser)));
            });
        } else if (pathInfo.equals("/userList")) {
            AdminUser adminUser = RequestUtils.getReqJsonBody(req, AdminUser.class);
            int pageNum = (req.getParameter("pageNum") == null || req.getParameter("pageNum") == "") ? 1 : Integer.parseInt(req.getParameter("pageNum"));
            int pageSize = (req.getParameter("pageSize") == null || req.getParameter("pageSize") == "") ? 10 : Integer.parseInt(req.getParameter("pageSize"));
            executeBusiness(resp, response -> {
                ResponseUtils.responseJSON(resp, AjaxResult.success(adminUserService.getUserPage(pageNum, pageSize, adminUser)));
            });
        }
    }
    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        if (!req.getParameterMap().containsKey("admin_id")) {
            ResponseUtils.responseJSON(resp, AjaxResult.error(400, "请传入admin_id"));
            return;
        }
        int id = Integer.parseInt(req.getParameter("admin_id"));
        executeBusiness(resp, response -> {
            ResponseUtils.responseJSON(resp, AjaxResult.success(adminUserService.deleteUser(id)));
        });
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminUser adminUser = RequestUtils.getReqJsonBody(req, AdminUser.class);
        executeBusiness(resp, response -> {
            ResponseUtils.responseJSON(resp, AjaxResult.success(adminUserService.updateUser(adminUser)));
        });
    }
}
